﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Service;
using QXBook.Controls.HtmlControls;

namespace Web.User
{
    public partial class MyFavorites : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测用户登录
            ServiceFactory.CreateInstance<UsersService>().CheckUserLogin();
            if (!Page.IsPostBack)
            {
                FavoritesCondition condition = new FavoritesCondition();

                condition.UserID = base.userInfo.UserID;

                this.TextBind(condition);
            }
        }

        private void TextBind(FavoritesCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<FavoritesInfo> list = ServiceFactory.CreateInstance<FavoritesService>().GetModelList(ref ps, condition);

            if (list.Count > 0)
            {
                this.rptList.DataSource = list;
                this.rptList.DataBind();

                phHasInfomation.Visible = true;
                phNoInfomation.Visible = false;
            }
            else
            {
                phHasInfomation.Visible = false;
                phNoInfomation.Visible = true;
            }
            //divPage.InnerHtml = ClientPager.CreatePager(ps);
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            FavoritesInfo favor = (FavoritesInfo)e.Item.DataItem;
            Anchor aTitle = (Anchor)e.Item.FindControl("aTitle");
            Literal litPrice = (Literal)e.Item.FindControl("litPrice");
            Literal litCount = (Literal)e.Item.FindControl("litCount");
            Literal litPublish = (Literal)e.Item.FindControl("litPublish");
            Literal litAddDate = (Literal)e.Item.FindControl("litAddDate");

            aTitle.Text = favor.Product.Title;

            litCount.Text = favor.Product.Sales.ToString();
            litPrice.Text = favor.Product.Price.ToString();
            litPublish.Text = favor.Product.IsPublish != YesNo.Yes ? "已下架" : string.Empty;

            litAddDate.Text = favor.AddDate.ToString("yyyy-MM-dd");
        }
    }
}